package com.heyqing.medium.day9;

/**
 * ClassName:LicenseKeyFormatting
 * Package:com.heyqing.medium.day9
 * Description:
 *
 * @Date:2024/10/11
 * @Author:Heyqing
 */
public class LicenseKeyFormatting {
    public String licenseKeyFormatting(String s, int k) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '-') continue;
            sb.append(c);
        }
        String all = sb.toString();
        int first = all.length() % k;
        int loop = all.length() / k;
        StringBuilder res = new StringBuilder();
        int count = 0;
        for (int i = 0; i <= loop; i++) {
            if (count >= all.length()) break;
            int node = k;
            if (i == 0 && first != 0) node = first;
            for (int j = 0; j < node; j++) {
                char w = all.charAt(count);
                if (w >= 'a' && w <= 'z') w = (char)(w - 32);
                res.append(w);
                count++;
            }
            if (count < all.length()) res.append('-');
        }
        return res.toString();
    }
}
